#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;




int main(){
	int n;
	cin>>n;
	int a[n];
	int dp[n][n];
	for(int i=0;i<n;i++){
		cin>>a[i];
		dp[i][i] = 0;
	}
	for(int len=2;len<=n;len++){
		for(int i=0;i<n;i++){
			dp[i][(i+len-1)%n] = 0;
			for(int k=0;k<len-1;k++){
				dp[i][(i+len-1)%n] = max(dp[i][(i+len-1)%n],dp[i][(i+k)%n]+dp[(i+k+1)%n][(i+len-1)%n]+a[i]*a[(i+k+1)%n]*a[(i+len)%n]);
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
		ans = max(ans,dp[i][(i+n-1)%n]);
	}
	cout<<ans;
}
